/*
Point,Circle,LineString,Polygon,Square,Box
*/
/** 
 * @param {string} source 
 * @param {string} type "Point,Circle,LineString,Polygon,Square,Box"
 */
function createDraw({ source, type = "Point" }) {
    let draw = null;
    let geometryFunction = null;
    let maxPoints = null;
    if (type == 'Square') {
        type = 'Circle'
        geometryFunction = ol.interaction.Draw.createRegularPolygon(4)
    } else if (type == 'Box') {
        type = 'LineString';
        geometryFunction = function (coordinates, geometry) {
            if (!geometry) {
                //多边形
                geometry = new ol.geom.Polygon(null)
            }
            var start = coordinates[0]
            var end = coordinates[1]
            geometry.setCoordinates([
                [start, [start[0], end[1]], end, [end[0], start[1]], start],
            ])
            return geometry
        }
        maxPoints = 2;
    }
    draw = new ol.interaction.Draw({
        type,
        source,
        geometryFunction,
        maxPoints
    })
    return draw;
}

export{createDraw}